package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.SaleCount;
import com.atguigu.jxc.entity.SaleData;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("saleListGoods")
@Slf4j
public class SaleListGoodsController {
    @Autowired
    private SaleListGoodsService saleListGoodsService;
    /**
     * 销售单保存
     * @param saleList
     * @param saleListGoodsStr
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(SaleList saleList, String saleListGoodsStr){
        return saleListGoodsService.save(saleList,saleListGoodsStr);
    }


    /**
     * 销售单据查询
     * @param saleNumber
     * @param customerId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(String saleNumber, Integer customerId, Integer state, String sTime,String eTime){
        List<SaleList> list = saleListGoodsService.list(saleNumber,customerId,state,sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    /**
     * 查询销售单据中的商品
     * @return
     */
    @PostMapping("goodsList")
    public Map<String,Object> goodsList(Integer saleListId){
        List<SaleListGoods> list = saleListGoodsService.goodsList(saleListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }


    /**
     * 删除销售单
     * @param saleListId
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(Integer saleListId){
        saleListGoodsService.delete(saleListId);
        return new ServiceVO(100,"请求成功");
    }

    /**
     * 支付结算（修改销售单付款状态）
     * @return
     */
    @PostMapping("/updateState")
    public ServiceVO updateState(Integer saleListId){
        saleListGoodsService.updateState(saleListId);
        return new ServiceVO(100,"请求成功");
    }

    /**
     * 销售统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @PostMapping("count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List<SaleCount> list = saleListGoodsService.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String res = gson.toJson(list);
        log.info("----------销售统计---------");
        log.info(res);
        return res;
    }

    /**
     * 按日统计接口
     * @return
     */
    @PostMapping("getSaleDataByDay")
    public String getSaleDataByDay(String sTime, String eTime){
        List<SaleData> list = saleListGoodsService.getSaleDateByDay(sTime,eTime);
        Gson gson = new Gson();
        String res = gson.toJson(list);
        log.info("------按月统计接口-------");
        log.info(res);
        return res;
    }

    /**
     * 按月统计接口
     * @param sTime
     * @param eTime
     * @return
     */
    @PostMapping("getSaleDataByMonth")
    public String getSaleDataByMonth(String sTime, String eTime){
        List<SaleData> list = saleListGoodsService.getSaleDateByMonth(sTime,eTime);
        Gson gson = new Gson();
        String res = gson.toJson(list);
        log.info("------按月统计接口-------");
        log.info(res);
        return res;
    }
}
